package C

import U.Utils
import rx.Observable

fun main(args: Array<String>) {

    concat()
    Utils.sleep(1200)
    shuffleConcat()
}

/**
 * concat 实际保证顺序的办法也很简单，根根传入的Observable的顺序 来发射而已
 */
fun concat() {
    val first = Observable.create(Observable.OnSubscribe<String> { t ->
        t.onNext("first")
        t.onCompleted()
    })
    val second = Observable.create(Observable.OnSubscribe<String> { t ->
        t.onNext("second")
        t.onCompleted()
    })
    val third = Observable.create(Observable.OnSubscribe<String> { t ->
        t.onNext("third")
        t.onCompleted()
    })

    Observable.concat(Observable.just(first, second, third)).subscribe({ println(it) })
}

fun shuffleConcat() {
    val first = Observable.create(Observable.OnSubscribe<String> { t ->
        t.onNext("first")
        t.onCompleted()
    })
    val second = Observable.create(Observable.OnSubscribe<String> { t ->
        t.onNext("second")
        t.onCompleted()
    })
    val third = Observable.create(Observable.OnSubscribe<String> { t ->
        t.onNext("third")
        t.onCompleted()
    })

    Observable.concat(Observable.just(second, third, first)).subscribe({ println(it) })
}